package com.school.controller;

import com.school.entity.Users;
import com.school.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/Users")
public class UsersController {
    @Autowired
    UsersService service;

    @ResponseBody
    @RequestMapping("/login.do")
    public boolean login(String user_phone, String user_password, HttpServletRequest request, HttpServletResponse response) {
        // response.setHeader("Access-Control-Allow-Origin", "*");//允许所有来源访同
        // response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");//允许访问的方式
        // response.setHeader("Access-Control-Max-Age", "3600");
        // response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        // System.out.println("===================================");
        // System.out.println("user_phone = " + user_phone);
        // System.out.println("user_password = " + user_password);
        Users users = service.selectOne(new Users(user_phone, user_password));
        if (users != null) {
            return true;
        }
        return false;
    }

    // , produces = "text/html;charset=UTF-8;"
    @ResponseBody
    @RequestMapping(value = "/register.do")
    public Users register(String user_phone, String user_password) {
        Users users = new Users(user_phone, user_password);
        System.out.println("users = " + users);
        int flag = service.insertOne(users);
        System.out.println("flag = " + flag);
        if (flag != 1) {
            return null;
        }
        return users;
    }

}
